package cn.iocoder.yudao.module.crossborder.platform.service.platformmapping;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformmapping.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformmapping.PlatformMappingDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformmapping.PlatformMappingMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商数据映射配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformMappingServiceImpl implements PlatformMappingService {

    @Resource
    private PlatformMappingMapper platformMappingMapper;

    @Override
    public Long createPlatformMapping(PlatformMappingSaveReqVO createReqVO) {
        // 插入
        PlatformMappingDO platformMapping = BeanUtils.toBean(createReqVO, PlatformMappingDO.class);
        platformMappingMapper.insert(platformMapping);

        // 返回
        return platformMapping.getId();
    }

    @Override
    public void updatePlatformMapping(PlatformMappingSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformMappingExists(updateReqVO.getId());
        // 更新
        PlatformMappingDO updateObj = BeanUtils.toBean(updateReqVO, PlatformMappingDO.class);
        platformMappingMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformMapping(Long id) {
        // 校验存在
        validatePlatformMappingExists(id);
        // 删除
        platformMappingMapper.deleteById(id);
    }

    @Override
        public void deletePlatformMappingListByIds(List<Long> ids) {
        // 删除
        platformMappingMapper.deleteByIds(ids);
        }


    private void validatePlatformMappingExists(Long id) {
        if (platformMappingMapper.selectById(id) == null) {
            throw exception(PLATFORM_MAPPING_NOT_EXISTS);
        }
    }

    @Override
    public PlatformMappingDO getPlatformMapping(Long id) {
        return platformMappingMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformMappingDO> getPlatformMappingPage(PlatformMappingPageReqVO pageReqVO) {
        return platformMappingMapper.selectPage(pageReqVO);
    }

}